import { Component, OnInit, Inject } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import {UtilityService} from '../../../service/utils.service';
import {appConfig} from '../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import { SettingsService } from '@delon/theme';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
import * as moment from 'moment';
import * as _ from 'lodash';

@Component({
    selector: 'app-sub-list',
    templateUrl: './sub-list.component.html',
    styleUrls: ['./sub-list.component.less'],
})
export class SubListComponent implements OnInit {

    constructor(
        private http: _HttpClient,
        private router: Router,
        private utilityService: UtilityService,
        private modal: NzModalService,
        private nznot: NzNotificationService,
        public settings: SettingsService,
        @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService, // 依赖注入 注入token
    ) { }

    // 变量
    loading = false;
    search: any;
    title: '请输入工作组代码';
    active: boolean;
    reset: boolean;
    isPagination: boolean;
    token: any; // 保存token信息
    bransguid: string
    deliveryTime: any; // 投放时间
    deliveryName: string; // 投放别名
    workItem: any; // 工作组List
    workItemInfo: any; // 工作组详情
    branchDetail: any; // 分支信息
    splicingObj: any; //  拼接的数据
    elementScice: any; // 环境数据
    infoVisible = false; //  弹出框默认关闭
    appendVisible = false; // 追加弹出框 默认关闭
    launchVisible = false; // 投放详情弹出框 默认关闭
    copysplicingObj: any; // 复制的数据 用来页面展示
    copytextList: any; // 复制的数据 用来页面展示
    isGou = false; // 默认是没有勾选的
    userName: string; // 用户名称
    itemName: string; // 工作组名称
    ifActive: boolean; // 是否请求成功
    copyinfos: any; // 拷贝的信息
    isShowTotal  = true; // 是否显示总数
    appendTitle: string; // 弹窗名称
    appendSelect: any; // 可追加的环境和方法
    selectApply = false; // 环境按钮
    isCaocuo = false;
    data: any[] = []; // 表格数据
    showAdd: boolean;
    test: string; // 测试
    page: any; // 分页传参格式
    total: number; // 总数
    modalVisible = false; // 投放申请弹框
    textcssList: any; // 修改的申请列表
    stashList: any; // 修改未投放的列表
    detailInfo: any; // 投放之后的详情
    profiles: any; // 提申请合并数组
    isOpen: boolean; // 是否显示投放按钮
    subtotal: boolean; // 是否显示总计
    isConfirmLoading = false; // 默认按钮loading为false
    titles = '正在读取数据...';
    selectedCRows = []//x修改提交的config选中
     selectedRows = []//x修改提交的普通选中
     workItmseach:any;
    ngOnInit() {
        this.active = false;
        this.reset = false;
        this.token  = this.tokenService.get().seniortoken; // 绑定token
        this.userName  = this.tokenService.get().name; // 绑定token
        this.copysplicingObj = {};
        this.copysplicingObj.dliveryAddRequest = {};
        this.copysplicingObj.dliveryAddRequest.profiles = [];
        this.copysplicingObj.deliveryList = [];
        this.subtotal = true;
    }



    // 调用初始化工作项信息
    getworkData() {
        this.utilityService.getData(appConfig.testUrl  + appConfig.API.sWorkitem)
            .subscribe(
                (val) => {
                    this.workItem = val.result;
                    if (val.result.length === 0) {
                        this.nznot.create('error', '暂无工作项' , '没有分配的工作项');
                    }
                }
            );
    }

    // 只要打开就调用工作项信息
    openChange() {
        this.getworkData(); // 调用工作项信息
    }


    // 下拉框选择
    checkSelect(event) {
        console.log(this.workItem)
        for ( var i = 0; i < this.workItem.length; i ++ ) {
            if (this.workItem[i].guid === event) {
                this.workItemInfo = this.workItem[i];
            }
        }
        // 把两个数据清空，防止影响
        // this.textcssList = undefined;
        // this.stashList = undefined;
        this.copyinfos = _.cloneDeep(this.workItemInfo.developers);
        this.workItemInfo.copyinfos = this.copyinfos.split(',');
        this.showAdd = true; // 默认没有新增
        // 请求工作项内容信息
        this.utilityService.getData(appConfig.testUrl  + appConfig.API.sWorkitem + '/' + event + '/branchDetail')
            .subscribe(
                (val) => {
                    this.branchDetail = val.result.fullPath;
                    this.bransguid = val.result.guid;
                    this.ifActive = true;
                    this.selectApply = true; // 显示
                    this.active = true; // 显示清单整理按钮
                },
                (src) => {
                    this.active = false; // 显示清单整理按钮
                    this.reset = false; // 分支信息关闭
                    this.selectApply = false; // 显示
                    this.nznot.create('error', src.code , src.msg);
                }
            );

        // 请求是否有清单
        this.utilityService.getData(appConfig.testUrl  + appConfig.API.sWorkitem + '/' + event + '/isOpen' )
            .subscribe(
                (val) => {
                    if (val.result.isOpen === false) {
                        this.isgetData = false
                        this.isOpen = false;
                    } else {
                          this.isgetData = true
                        this.isOpen = true;

                    }
                },
                (src) => {
                      this.nznot.create('error', src.code , src.msg);
                }
            );

        this.getcheckOptionOne(this.workItemInfo.guid); // 调用清单
        this.reset  = false;

    }


    // 整理清单
    listreset() {
        this.titles = '正在读取数据...'
        if (this.ifActive) { // 请求回来之后在打开页面
            this.isPagination = false;
            this.itemName = this.workItemInfo.itemName; // 绑定工作项名称
            this.loading = true;
            this.selectApply = true; // 显示
          
            this.getData();

        }

    }

    // 传入按钮层
    moreData = {
        morebutton: true,
        buttons: [
            { key: 'Overview', value: '查看概况' }
        ]
    }

    // 数组转换特定格式
    arrarObj(event) {
        let listArray = [];
        for ( let i = 0 ; i <  event.length;  i++) {
            let obj = {
                value: event[i],
                label: event[i],
            };
            listArray.push(obj);
        }
        return listArray;
    }

    // 初始化list
    loadingList = true;
    isShowtext = false;
    isShowtexterror = false;
    CallChecked = true;
   allChecked = true;
    getData() {
        // 请求信息
        this.isShowtext = true;
        this.flage = false;
        this.flageUpdConfig = false;
        this.flageConfig = false;
        this.flageUpd = false;
        let arrconfig = []
        let arr = []
        this.textcssList = []
        this.stashList = []
        this.loadingList = true;
        this.selectedRows = []
        
        this.utilityService.getData(appConfig.testUrl  + appConfig.API.sDeliveryList + '/'+ this.bransguid + '/history')
            .subscribe(
                (val) => {
                 
                    this.isOpen = true;
                    this.isShowtexterror = false;
                    this.isShowtext = false;
                    this.loadingList = false;
                    this.loading = false;
                    this.reset = true; // 打开右侧内容
                    this.textcssList = val.result.deliveryDetail;
                    this.stashList = val.result.stashDetail;
                    let index = 0;
                    console.log(this.textcssList)
                    // 整理出来的清单
                    for (let i = 0; i < this.textcssList.length; i ++) {
                        this.textcssList[i].selected = true;
                        
                        if (this.textcssList[i].projectType === 'S' ) { // 说明是config工程，需要让用户手动选择
                         
                           arrconfig.push(this.textcssList[i].projectType)
                            this.textcssList[i].headerData = [  // 配置表头内容
                                { label: '程序名称', value: 'fullName', isclick: true, radio: false },
                                { label: '变动类型', value: 'commitType', isclick: false, radio: false  },
                                { label: '导出为', value: 'patchType', isclick: false, radio: true, type: 'patchArray'  },
                                { label: '部署为', value: 'deployWhere', isclick: false, radio: true, type: 'deployArray' },
                            ];
                            for (let j = 0; j < this.textcssList[i].deliveryPatchDetails.length; j++) {
                            
                               
                                for ( let n = 0; n < this.textcssList[i].deliveryPatchDetails[j].fileList.length; n++) {
                                    // 截取字符串
                                    index = this.textcssList[i].deliveryPatchDetails[j].fileList[n].fullPath.indexOf(this.textcssList[i].deliveryPatchDetails[j].fileList[n].partOfProject);
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].fullName = this.textcssList[i].deliveryPatchDetails[j].fileList[n].fullPath.substring(index, this.textcssList[i].deliveryPatchDetails[j].fileList[n].fullPath.length);
                                    // 拿到渲染结构
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].depolyJsonArray = _.cloneDeep(JSON.parse(this.textcssList[i].deliveryPatchDetails[j].fileList[n].deployWhere));
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchArray = this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchType.split(',');
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].deployArray = []; // 先等于空数组,push进去即可
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchArray = this.arrarObj(this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchArray);
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].checked = true; // 默认请求
                                  
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].commitDate = moment(this.textcssList[i].deliveryPatchDetails[j].fileList[n].commitDate).format('YYYY-MM-DD HH:mm:ss');
                                }
                               
                            }
                        } else {
                            arr.push(this.textcssList[i].projectType)
                           
                            this.textcssList[i].headerData = [  // 配置表头内容
                                { label: '程序名称', value: 'fullName', isclick: true, radio: false },
                                { label: '变动类型', value: 'commitType', isclick: false, radio: false },
                            ];
                            this.flageConfig = true
                            for (let j = 0; j < this.textcssList[i].deliveryPatchDetails.length; j++) {
                                this.textcssList[i].deliveryPatchDetails[j].selected = true;
                                for ( let n = 0; n < this.textcssList[i].deliveryPatchDetails[j].fileList.length; n++) {
                                       
                                    // 截取字符串
                                    index = this.textcssList[i].deliveryPatchDetails[j].fileList[n].fullPath.indexOf(this.textcssList[i].deliveryPatchDetails[j].fileList[n].partOfProject);
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].fullName = this.textcssList[i].deliveryPatchDetails[j].fileList[n].fullPath.substring(index, this.textcssList[i].deliveryPatchDetails[j].fileList[n].fullPath.length);
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].checked = true; // 默认请求
                                    this.textcssList[i].deliveryPatchDetails[j].fileList[n].commitDate = moment(this.textcssList[i].deliveryPatchDetails[j].fileList[n].commitDate).format('YYYY-MM-DD HH:mm:ss');
                                }
                            }
                        }
                    }
                  
                  if(arr.length > 0 ){
                      this.flage = true;
                     
                  }else{
                       this.flage = false;
                       
                  }
                  if(arrconfig.length > 0){
                      this.flageConfig = true;
                      
                  }else{
                       this.flageConfig = false;
                      
                  }
                //   this.checkFlage();
            
              
                
                 
                    // 修改但是没提交的清单
                    for (let i = 0; i < this.stashList.length; i ++) {
                         this.stashList[i].selected = true;
                        if (this.stashList[i].projectType === 'S') { // 说明是config工程，需要让用户手动选择
                            this.stashList[i].headerData = [  // 配置表头内容
                                { label: '程序名称', value: 'fullName', isclick: true, radio: false },
                                { label: '变动类型', value: 'commitType', isclick: false, radio: false  },
                                { label: '导出为', value: 'patchType', isclick: false, radio: true, type: 'patchArray'  },
                                { label: '部署为', value: 'deployWhere', isclick: false, radio: true, type: 'deployArray' },
                            ];
                            for (let j = 0; j < this.stashList[i].deliveryPatchDetails.length; j++) {
                                
                                for ( let n = 0; n < this.stashList[i].deliveryPatchDetails[j].fileList.length; n++) {
                                    // 截取字符串
                                    index = this.stashList[i].deliveryPatchDetails[j].fileList[n].fullPath.indexOf(this.stashList[i].deliveryPatchDetails[j].fileList[n].partOfProject);
                                    this.stashList[i].deliveryPatchDetails[j].fileList[n].fullName = this.stashList[i].deliveryPatchDetails[j].fileList[n].fullPath.substring(index, this.stashList[i].deliveryPatchDetails[j].fileList[n].fullPath.length);
                                    // 拿到渲染结构
                                    this.stashList[i].deliveryPatchDetails[j].fileList[n].depolyJsonArray = _.cloneDeep(JSON.parse(this.stashList[i].deliveryPatchDetails[j].fileList[n].deployWhere));
                                    this.stashList[i].deliveryPatchDetails[j].fileList[n].patchArray = this.stashList[i].deliveryPatchDetails[j].fileList[n].patchType.split(',');
                                    this.stashList[i].deliveryPatchDetails[j].fileList[n].deployArray = []; // 先等于空数组,push进去即可
                                    this.stashList[i].deliveryPatchDetails[j].fileList[n].patchArray = this.arrarObj(this.stashList[i].deliveryPatchDetails[j].fileList[n].patchArray);
                                    this.stashList[i].deliveryPatchDetails[j].fileList[n].commitDate = moment(this.stashList[i].deliveryPatchDetails[j].fileList[n].commitDate).format('YYYY-MM-DD HH:mm:ss');
                               
                             }
                            }
                        } else {
                            this.stashList[i].headerData = [  // 配置表头内容
                                { label: '程序名称', value: 'fullName', isclick: true, radio: false },
                                { label: '变动类型', value: 'commitType', isclick: false, radio: false },
                            ];
                            for (let j = 0; j < this.stashList[i].deliveryPatchDetails.length; j++) {
                                 this.stashList[i].deliveryPatchDetails[j].selected = true;
                                for ( let n = 0; n < this.stashList[i].deliveryPatchDetails[j].fileList.length; n++) {
                                    // 截取字符串
                                    index = this.stashList[i].deliveryPatchDetails[j].fileList[n].fullPath.indexOf(this.stashList[i].deliveryPatchDetails[j].fileList[n].partOfProject);
                                    this.stashList[i].deliveryPatchDetails[j].fileList[n].fullName = this.stashList[i].deliveryPatchDetails[j].fileList[n].fullPath.substring(index, this.stashList[i].deliveryPatchDetails[j].fileList[n].fullPath.length);
                                    this.stashList[i].deliveryPatchDetails[j].fileList[n].commitDate = moment(this.stashList[i].deliveryPatchDetails[j].fileList[n].commitDate).format('YYYY-MM-DD HH:mm:ss');
                                }
                            }
                        }
                    }
                    console.log(this.textcssList)
             

                },
                (error) => {
                    
                    this.loading = false;
                    this.isShowtexterror = true;
                    this.isShowtext = false;
                    this.titles = error.msg;
                    this.nznot.create('error', error.code , error.msg);
                }
            );
    }

    // 按钮点击事件
    buttonEvent(event) {
        if (event.data.names === '删除') {
        } else {
        }

    }

    // 列表按钮方法
    buttonDataHandler(event) {
    }


    // 处理行为代码，跳转、弹出框、其他交互
    isActive(event) {

    }
   flageConfig:boolean;
   flage:boolean;
    checkListTesxt(){
          this.flage = false;
        for (let i = 0; i < this.textcssList.length; i ++) {
              
               for (let j = 0; j < this.textcssList[i].deliveryPatchDetails.length; j++) {
                     if(this.textcssList[i].deliveryPatchDetails[j].selected === true){
                         this.textcssList[i].selected = true
                         break;
                     } 
               }
          }
        
           for (let i = 0; i < this.textcssList.length; i ++) {

           if (this.textcssList[i].projectType !== 'S' && this.textcssList[i].selected === true) {
                              this.flage = true;
                              break;
               } 
            
          }
         
           

   }
   checkListTesxtConfig(){
        this.flageConfig = false;
        for (let i = 0; i < this.textcssList.length; i ++) {
               if (this.textcssList[i].projectType === 'S' && this.textcssList[i].selected === true) {
                              this.flageConfig = true;
                              break;
               }
            
          }
   }
    checkListstashConfig(){
          this.flageUpdConfig = false;
      for (let i = 0; i < this.stashList.length; i ++) {
           if (this.stashList[i].projectType === 'S' && this.stashList[i].selected === true) {
                              this.flageUpdConfig = true;
                              break;
               }
        
           }
   }
   checkListstash(){
       this.flageUpd = false;
         for (let i = 0; i < this.stashList.length; i ++) {
              
               for (let j = 0; j < this.stashList[i].deliveryPatchDetails.length; j++) {
                     if(this.stashList[i].deliveryPatchDetails[j].selected === true){
                         this.stashList[i].selected = true
                         break;
                     } 
               }
          }
      for (let i = 0; i < this.stashList.length; i ++) {
               if (this.stashList[i].projectType !== 'S' && this.stashList[i].selected === true) {
                              this.flageUpd = true;
                              break;
               }
           }
   }
    selectedRow(event,s,i) { // 这是修改提交普通工程
        console.log('这是修改提交普通工程')
          
             
               i.selected =false;
        if(event.selectedRows.length === 0){
           
           s.selected = false;
        }else{
            s.selected = true;
           
        }

        this.checkListTesxt()
  
    }
 
    selectedRowConfig(event,i) { // 这是修改提交Config工程
                console.log('这是修改提交Config工程')
            console.log(event)
        if(event.selectedRows.length === 0){
         
             i.selected = false;
        }else{
        
            i.selected = true;
        }
        this.checkListTesxtConfig()
               

    }

    flageUpdConfig:boolean;
    selectedRowUpdConfig(event,i) { // 这是修改未提交Config工程
           console.log('这是修改未提交Config工程')
       
        if(event.selectedRows.length === 0){
          
             i.selected = false;
        }else{
        
           i.selected =  true;

        }
        this.checkListstashConfig()
             console.log(this.flageUpdConfig)
    console.log(i)
        

    }
    flageUpd:boolean;
    selectedRowUpd(event,s,i) { //这是修改未提交普通工程
               console.log('这是修改未提交普通工程')
        i.selected = false;
        if(event.selectedRows.length === 0){
       
           s.selected = false;
        }else{
   
           s.selected = true;
        }
     

          this.checkListstash()
             
    
    }

    // 补录清单方法
    Supplementary() {
    }

    // 投放申请

    Serve() {


        let array = []; // 定义数组, 每次进入之前先清空
      
     
        
        if ((_.isUndefined(this.textcssList) || this.textcssList.length === 0|| (this.flage === false && this.flageConfig === false)) && (_.isUndefined(this.stashList) || this.stashList.length === 0 || (this.flageUpd === false && this.flageUpdConfig === false)) ) {
            let array = []; // 定义数组, 每次进入之前先清空
            this.modalVisible = true;
            // 不选清单, 禁选
            for (let i = 0; i < this.elementScice.length; i ++) {
                if (this.elementScice[i].delivered) {
                    this.elementScice[i].disabled = true;
                }
            }
        } else {
            let array = []; // 定义数组, 每次进入之前先清空
            // 修改的判断逻辑
            for (let i = 0; i < this.textcssList.length; i ++) {
                // 代表config工程
                if (this.textcssList[i].projectType === 'S') { // 说明是config或者default工程，需要让用户手动选择
                    for (let j = 0; j < this.textcssList[i].deliveryPatchDetails.length; j++) {
                        for (let n = 0; n < this.textcssList[i].deliveryPatchDetails[j].fileList.length; n++) {
                            if (this.textcssList[i].deliveryPatchDetails[j].fileList[n].checked) {
                                if (this.textcssList[i].deliveryPatchDetails[j].fileList[n].deploySelect && this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchSelect) {
                                    array.push(this.textcssList[i].deliveryPatchDetails[j].fileList[n])
                                }
                            }
                        }
                    }
                } else { // 普通工程
                    for (let j = 0; j < this.textcssList[i].deliveryPatchDetails.length; j++) {
                        for (let n = 0; n < this.textcssList[i].deliveryPatchDetails[j].fileList.length; n++) {
                            if (this.textcssList[i].deliveryPatchDetails[j].fileList[n].checked) {
                                array.push(this.textcssList[i].deliveryPatchDetails[j].fileList[n])
                            }
                        }

                    }
                }
            }

            if (this.stashList.length !== 0 ) {
                // 修改的未提交的判断逻辑
                for (let i = 0; i < this.stashList.length; i ++) {
                    if (this.stashList[i].projectType === 'S') { // 说明是config或者default工程，需要让用户手动选择
                        for (let j = 0; j < this.stashList[i].deliveryPatchDetails.length; j++) {
                            for (let n = 0; n < this.stashList[i].deliveryPatchDetails[j].fileList.length; n++) {
                                if (this.stashList[i].deliveryPatchDetails[j].fileList[n].checked) {
                                    if (this.stashList[i].deliveryPatchDetails[j].fileList[n].deploySelect && this.stashList[i].deliveryPatchDetails[j].fileList[n].patchSelect) {
                                        array.push(this.stashList[i].deliveryPatchDetails[j].fileList[n]);
                                    }
                                }
                            }
                        }
                    } else {
                        for (let j = 0; j < this.stashList[i].deliveryPatchDetails.length; j++) {
                            for (let n = 0; n < this.stashList[i].deliveryPatchDetails[j].fileList.length; n++) {
                                if (this.stashList[i].deliveryPatchDetails[j].fileList[n].checked) {
                                    array.push(this.stashList[i].deliveryPatchDetails[j].fileList[n]);
                                }
                            }

                        }
                    }
                }
            }

            // if (array.length > 0) {
                
                this.modalVisible = true;
                // 选清单, 必选
                for (let i = 0; i < this.elementScice.length; i ++) {
                    if (this.elementScice[i].delivered) {
                        this.elementScice[i].disabled = true;
                        this.elementScice[i].check = true;
                    }
                }
               
      
        }
        this.deliveryTime = moment(new Date()).format('YYYY-MM-DD');
         this.getLoading = false;
    }
   
    getLoading = false;
    OkText = '确定'
    isgetData:boolean;
    getServe(){
      this.getLoading = true;
       if (!this.flage && !this.flageConfig && !this.flageUpd && !this.flageUpdConfig){
           if(this.isgetData){
              this.OkText = '确定'
              this.getStatus = true;
              this.Serve();
            }else{
                this.getLoading = false;
                this.nznot.create('error','当前工作项没有需要提交的代码','')
              }
            
      }else{
                    
                let isGOUconfig = false
                 
                  for (let i = 0; i < this.textcssList.length; i ++) {
                          
                                if (this.textcssList[i].projectType === 'S' ) { // 说明是config工程，需要让用户手动选择
                                    for (let j = 0; j < this.textcssList[i].deliveryPatchDetails.length; j++) {
                                        let arr = []
                                        for ( let n = 0; n < this.textcssList[i].deliveryPatchDetails[j].fileList.length; n++) {
                                            if (this.textcssList[i].deliveryPatchDetails[j].fileList[n].checked) {
                                                 console.log(this.textcssList[i].deliveryPatchDetails[j].fileList[n].deploySelect)
                                                 console.log(this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchSelect)
                                                if ( _.isUndefined(this.textcssList[i].deliveryPatchDetails[j].fileList[n].deploySelect)|| _.isUndefined(this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchSelect) || _.isNull(this.textcssList[i].deliveryPatchDetails[j].fileList[n].deploySelect)||_.isNull(this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchSelect) ) {
                                                       this.nznot.create('error', '请检查修改提交的Config工程是否勾选导出和部署' ,'');
                                                   this.getLoading = false;
                                                     console.log('跳出');
                                                     return;
                                             
                                                    }                                                 
                                              
                                            }
                                        }
                                    }
                                }
                            }
                      let isGou = false;
                                  for (let i = 0; i < this.stashList.length; i ++) {
                                       if (this.stashList[i].projectType ==='S' ) { 
                                       for (let j = 0; j < this.stashList[i].deliveryPatchDetails.length; j++) {
                                          for ( let n = 0; n < this.stashList[i].deliveryPatchDetails[j].fileList.length; n++) {
                                                    if (this.stashList[i].deliveryPatchDetails[j].fileList[n].checked) {
                                                       if (_.isUndefined(this.stashList[i].deliveryPatchDetails[j].fileList[n].deploySelect) || _.isUndefined(this.stashList[i].deliveryPatchDetails[j].fileList[n].patchSelect) || _.isNull(this.stashList[i].deliveryPatchDetails[j].fileList[n].deploySelect) || _.isNull(this.stashList[i].deliveryPatchDetails[j].fileList[n].patchSelect)) {
                                                         this.nznot.create('error', '请检查修改未提交的Config工程是否勾选导出和部署' ,'');
                                                        this.getLoading = false;
                                                            console.log('跳出');
                                                            return;

                                                   }
                                          }
                                       }
                                 }
                                }
                            } 
                 
                   this.OkText = '确定'
            
                      let url = '/deliveries/deliveredWay/'+this.workItemInfo.guid;
                            this.utilityService.getData(appConfig.testUrl  + url)
                                .subscribe(
                                    (val) => {
                                    let str =  JSON.parse(val.result);
                                  
                                    switch(str.result){
                                        case 'D':
                                        this.OkText = '确定'
                                        this.getStatus = true;
                                        this.Serve();
                                        break;
                                        case 'S':
                                        this.appendSave();
                                        break;
                                        default:
                                        this.OkText = '作废'
                                        this.getStatus = false;
                                        this.getLose()
                                        break;
                                    }
                                    }
                                    ,(error)=>{
                                        this.getLoading = false;
                                    this.nznot.create('error', error.code , error.msg);
                                    })
                        
            
                               
                            }
        
       
    }
    getStatus:boolean;
    getGuid = []
    getLose(){
        this.getGuid = []
     let url='/deliveries/applyingDelivered/'+this.workItemInfo.guid;
      this.utilityService.getData(appConfig.testUrl  + url )
            .subscribe(
                (val) => {
                    
                     this.getLoading = false;
                    this.detailInfo = val.result;
                    this.detailInfo.forEach(i=>{
                      this.getGuid.push(i.guid)
                    })
                    this.modalVisible = false; // 关闭
                    this.launchVisible  = true; // 显示详情
                    this.reset = false;
                    this.textcssList = undefined;// 投放申请list
                    this.stashList = undefined;// 
                    this.getcheckOptionOne(this.workItemInfo.guid); // 重新调用清单
                    this.isConfirmLoading = false;
                },(error)=>{
                   this.getLoading = false;
                this.nznot.create('error', error.code , error.msg);
                })
    }
    getFalse(){
      if(this.getStatus === true){
         this.launchVisible = false;
      }else{
          console.log(this.getStatus)
        let url='/deliveries/buyFailed';
             this.utilityService.postData(appConfig.testUrl  + url,this.getGuid)
            .subscribe(
                (val) => {
                   this.nznot.create('success',val.code ,val.msg);
                   this.launchVisible = false;
                     this.flage = false;
                    this.flageConfig = false;
                    this.flageUpd = false;
                    this.flageUpdConfig = false;
                      if(!this.isgetData){
                        this.isOpen = false;
                    }
                    // this.checkFlage()    
                    this.textcssList = [];
                    this.stashList = [];
                },(error)=>{
                this.nznot.create('error', error.code , error.msg);
                })
      }
    }
    // 数据处理
    dataChange() {
        /*拼数据*/
        let objsss = false; // 前端判定是否正确
        let array = []; // 选中的
        let noarray = []; // 未选中的
        // 修改的判断
        if (!_.isUndefined(this.textcssList)) { // 是否存在
            let cloneText = _.cloneDeep(this.textcssList);
            for (let i = 0; i < cloneText.length; i ++) {
                if (cloneText[i].projectType  !== 'S' ) { // 普通工程
                    for (let j = 0; j < cloneText[i].deliveryPatchDetails.length; j++) {
                        for ( let n = 0; n < cloneText[i].deliveryPatchDetails[j].fileList.length; n++) {
                            if (cloneText[i].deliveryPatchDetails[j].fileList[n].checked) {
                                if ( !_.isUndefined(cloneText[i].deliveryPatchDetails[j].fileList[n].deployWhere) && !_.isUndefined(cloneText[i].deliveryPatchDetails[j].fileList[n].patchSelect)) {
                                    cloneText[i].deliveryPatchDetails[j].fileList[n].deployWhere = cloneText[i].deliveryPatchDetails[j].fileList[n].deploySelect;
                                    cloneText[i].deliveryPatchDetails[j].fileList[n].patchType = cloneText[i].deliveryPatchDetails[j].fileList[n].patchSelect;
                                }
                                // 处理转换逻辑，把radio选中的给后台
                                array.push(cloneText[i].deliveryPatchDetails[j].fileList[n]);
                            } else {
                                noarray.push(this.textcssList[i].deliveryPatchDetails[j].fileList[n]);
                            }
                        }
                    }
                } else {
                    for (let j = 0; j < cloneText[i].deliveryPatchDetails.length; j++) {
                        for ( let n = 0; n < cloneText[i].deliveryPatchDetails[j].fileList.length; n++) {
                            if (cloneText[i].deliveryPatchDetails[j].fileList[n].checked) {
                                if ( !_.isUndefined(cloneText[i].deliveryPatchDetails[j].fileList[n].deployWhere) && !_.isUndefined(cloneText[i].deliveryPatchDetails[j].fileList[n].patchSelect)) {
                                    cloneText[i].deliveryPatchDetails[j].fileList[n].deployWhere = cloneText[i].deliveryPatchDetails[j].fileList[n].deploySelect;
                                    cloneText[i].deliveryPatchDetails[j].fileList[n].patchType = cloneText[i].deliveryPatchDetails[j].fileList[n].patchSelect;
                                }
                                array.push(cloneText[i].deliveryPatchDetails[j].fileList[n]);
                            } else {
                                noarray.push(this.textcssList[i].deliveryPatchDetails[j].fileList[n]);
                            }
                        }
                    }
                }

            }
        }
        // 未提交的判断
        if (!_.isUndefined(this.stashList)) { // 是否存在
            let cloneText = _.cloneDeep(this.stashList);
            for (let i = 0; i < cloneText.length; i ++) {
                if (cloneText[i].projectType !== 'S' ) { // 普通工程
                    for (let j = 0; j < cloneText[i].deliveryPatchDetails.length; j++) {
                        for ( let n = 0; n < cloneText[i].deliveryPatchDetails[j].fileList.length; n++) {
                            if (cloneText[i].deliveryPatchDetails[j].fileList[n].checked) {
                                if ( !_.isUndefined(cloneText[i].deliveryPatchDetails[j].fileList[n].deployWhere) && !_.isUndefined(cloneText[i].deliveryPatchDetails[j].fileList[n].patchSelect)) {
                                    cloneText[i].deliveryPatchDetails[j].fileList[n].deployWhere = cloneText[i].deliveryPatchDetails[j].fileList[n].deploySelect;
                                    cloneText[i].deliveryPatchDetails[j].fileList[n].patchType = cloneText[i].deliveryPatchDetails[j].fileList[n].patchSelect;
                                }
                                // 处理转换逻辑，把radio选中的给后台
                                array.push(cloneText[i].deliveryPatchDetails[j].fileList[n]);
                            } else {
                                noarray.push(this.stashList[i].deliveryPatchDetails[j].fileList[n]);
                            }
                        }
                    }
                } else {
                    for (let j = 0; j < cloneText[i].deliveryPatchDetails.length; j++) {
                        for ( let n = 0; n < cloneText[i].deliveryPatchDetails[j].fileList.length; n++) {
                            if (cloneText[i].deliveryPatchDetails[j].fileList[n].checked) {
                                if ( !_.isUndefined(cloneText[i].deliveryPatchDetails[j].fileList[n].deployWhere) && !_.isUndefined(cloneText[i].deliveryPatchDetails[j].fileList[n].patchSelect)) {
                                    cloneText[i].deliveryPatchDetails[j].fileList[n].deployWhere = cloneText[i].deliveryPatchDetails[j].fileList[n].deploySelect;
                                    cloneText[i].deliveryPatchDetails[j].fileList[n].patchType = cloneText[i].deliveryPatchDetails[j].fileList[n].patchSelect;
                                }
                                array.push(cloneText[i].deliveryPatchDetails[j].fileList[n]);
                            } else {
                                noarray.push(this.stashList[i].deliveryPatchDetails[j].fileList[n]);
                            }
                        }
                    }
                }

            }
        }



        this.splicingObj = {
            guidBranch : this.bransguid,
            dliveryAddRequest: {
                profiles: this.profiles,
            },
            guidWorkitem: this.workItemInfo.guid,
            deliveryList: array,
            stashList: noarray
        };
    }
    monitorHandler($event){
        
    }

    // 投放申请
    save() {
        this.isConfirmLoading = true;
      
        this.profiles = [];
        for (let i = 0; i < this.elementScice.length; i ++) {
            if (this.elementScice[i].check && this.elementScice[i].times) {
                if (_.isUndefined(this.elementScice[i].deliveryName)) { // 如果是undefined, 那么就给一个默认的别名
                    let obj = {
                        guidProfiles: this.elementScice[i].guidProfile,
                        profilesName: this.elementScice[i].profilesName,
                        packTiming: this.elementScice[i].times,
                        name: this.elementScice[i].manager,
                        applyAlias: this.workItemInfo.itemName, // 默认别名 工作项名称
                        deliveryTime: moment(this.elementScice[i].deliveryTime).format('YYYY-MM-DD')
                    };
                    this.profiles.push(obj);
                } else {
                    let obj = {
                        guidProfiles: this.elementScice[i].guidProfile,
                        profilesName: this.elementScice[i].profilesName,
                        packTiming: this.elementScice[i].times,
                        name: this.elementScice[i].manager,
                        applyAlias: this.elementScice[i].deliveryName,
                        deliveryTime: moment(this.elementScice[i].deliveryTime).format('YYYY-MM-DD')
                    };
                    this.profiles.push(obj);
                }

            }
        }
        this.splicingObj = null;
        this.dataChange();
        // 枚举值改变
        for ( let i = 0; i < this.splicingObj.deliveryList.length; i++) {
            if (this.splicingObj.deliveryList[i].commitType === '新增') {
                this.splicingObj.deliveryList[i].commitType = 'A';
            } else if (this.splicingObj.deliveryList[i].commitType === '删除') {
                this.splicingObj.deliveryList[i].commitType = 'D';
            }else if (this.splicingObj.deliveryList[i].commitType === '修改') {
                this.splicingObj.deliveryList[i].commitType = 'M';
            }

            if (this.splicingObj.deliveryList[i].fromType === '手动补录') {
                this.splicingObj.deliveryList[i].fromType = 'A';
            } else if (this.splicingObj.deliveryList[i].fromType === '分支提交') {
                this.splicingObj.deliveryList[i].fromType = 'B';
            }else if (this.splicingObj.deliveryList[i].fromType === '标准清单') {
                this.splicingObj.deliveryList[i].fromType = 'S';
            } else {
                this.splicingObj.deliveryList[i].fromType = 'M';
            }
        }
        for ( let i = 0; i < this.splicingObj.stashList.length; i++) {
            if (this.splicingObj.stashList[i].commitType === '新增') {
                this.splicingObj.stashList[i].commitType = 'A';
            } else if (this.splicingObj.stashList[i].commitType === '删除') {
                this.splicingObj.stashList[i].commitType = 'D';
            }else if (this.splicingObj.stashList[i].commitType === '修改') {
                this.splicingObj.stashList[i].commitType = 'M';
            }

            if (this.splicingObj.stashList[i].fromType === '手动补录') {
                this.splicingObj.stashList[i].fromType = 'A';
            } else if (this.splicingObj.stashList[i].fromType === '分支提交') {
                this.splicingObj.stashList[i].fromType = 'B';
            }else if (this.splicingObj.stashList[i].fromType === '标准清单') {
                this.splicingObj.stashList[i].fromType = 'S';
            } else {
                this.splicingObj.stashList[i].fromType = 'M';
            }
        }

        this.utilityService.postData(appConfig.testUrl  + appConfig.API.sDeliveryList +  '/deliveryAndDeliveryList', this.splicingObj,{})
            // .map(res => res.json())
            .subscribe(
                (suc) => {
                    this.nznot.create('success', suc.code , suc.msg);
                    this.detailInfo = suc.result;
                    this.appendTitle = '投放成功';
                    this.modalVisible = false; // 关闭
                    this.launchVisible  = true; // 显示详情
                    // this.getData();
                    this.reset = false;
                    this.flage = false;
                    this.flageConfig = false;
                    this.flageUpd = false;
                    this.flageUpdConfig = false;
                    if(!this.isgetData){
                        this.isOpen = false;
                    }
                    // this.checkFlage();
                    this.textcssList = [];
                    this.stashList = [];
                    this.getcheckOptionOne(this.workItemInfo.guid); // 重新调用清单
                    this.isConfirmLoading = false;
                },
                (error) => {
                    this.nznot.create('error', error.code , error.msg);
                    this.isConfirmLoading = false;
                    // this.getData();
                }
            );
    }

    subSave() {
    }
 
    // 追加代码逻辑
    appSaveTitle = '当前工作项存在申请中的投放记录，请追加！'
    appendSave() {
            this.utilityService.getData(appConfig.testUrl  + '/deliveries/' +  + this.workItemInfo.guid + '/addTo' )
                .subscribe(
                    (val) => {
                        this.getLoading = false;
                        this.appSaveTitle = '工作项[ '+ this.workItemInfo.itemName+' ] 存在申请中的投放记录，请追加！'
                        this.appendSelect = val.result;
                        if (this.appendSelect.length > 0 ) {
                            this.appendVisible = true;
                        } else {
                            this.nznot.create('error', '没有可追加的清单', '没有可追加的清单');
                        }

                    },(error)=>{
                         this.getLoading = false;
                         this.nznot.create('error', error.code, error.msg);
                    }
                );

        

    }

    // 追加代码确认
    getFalseLoading = false;
    appendSavetwo() {
        let submitArray = [];
        _.forEach(this.appendSelect , function (select) {
           submitArray.push(select.guid); // guid 集合
        })
        let array = []; // 选中的
        let noarray = []; // 未选中的
        // 修改的
        for (let i = 0; i < this.textcssList.length; i ++) {
            if (this.textcssList[i].projectType === 'S') { // 说明是config工程，需要让用户手动选择
                for (let j = 0; j < this.textcssList[i].deliveryPatchDetails.length; j++) {
                    for ( let n = 0; n < this.textcssList[i].deliveryPatchDetails[j].fileList.length; n++) {
                        if (this.textcssList[i].deliveryPatchDetails[j].fileList[n].checked) {
                            if ( !_.isUndefined(this.textcssList[i].deliveryPatchDetails[j].fileList[n].deployWhere) && !_.isUndefined(this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchSelect)) {
                                this.textcssList[i].deliveryPatchDetails[j].fileList[n].deployWhere = this.textcssList[i].deliveryPatchDetails[j].fileList[n].deploySelect;
                                this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchType = this.textcssList[i].deliveryPatchDetails[j].fileList[n].patchSelect;
                            }
                            // 处理转换逻辑，把radio选中的给后台
                            array.push(this.textcssList[i].deliveryPatchDetails[j].fileList[n]);
                        } else {
                            noarray.push(this.textcssList[i].deliveryPatchDetails[j].fileList[n]);
                        }

                    }
                }
            } else {
                for (let j = 0; j < this.textcssList[i].deliveryPatchDetails.length; j++) {
                    for ( let n = 0; n < this.textcssList[i].deliveryPatchDetails[j].fileList.length; n++) {
                        if (this.textcssList[i].deliveryPatchDetails[j].fileList[n].checked) {
                            array.push(this.textcssList[i].deliveryPatchDetails[j].fileList[n]);
                        } else {
                            noarray.push(this.textcssList[i].deliveryPatchDetails[j].fileList[n]);
                        }
                    }
                }
            }

        }
        // 修改未提交的
        for (let i = 0; i < this.stashList.length; i ++) {
            if (this.stashList[i].projectType === 'S') { // 说明是config工程，需要让用户手动选择
                for (let j = 0; j < this.stashList[i].deliveryPatchDetails.length; j++) {
                    for ( let n = 0; n < this.stashList[i].deliveryPatchDetails[j].fileList.length; n++) {
                        if (this.stashList[i].deliveryPatchDetails[j].fileList[n].checked) {
                            if ( !_.isUndefined(this.stashList[i].deliveryPatchDetails[j].fileList[n].deployWhere) && !_.isUndefined(this.stashList[i].deliveryPatchDetails[j].fileList[n].patchSelect)) {
                                this.stashList[i].deliveryPatchDetails[j].fileList[n].deployWhere = this.stashList[i].deliveryPatchDetails[j].fileList[n].deploySelect;
                                this.stashList[i].deliveryPatchDetails[j].fileList[n].patchType = this.stashList[i].deliveryPatchDetails[j].fileList[n].patchSelect;
                            }
                            // 处理转换逻辑，把radio选中的给后台
                            array.push(this.stashList[i].deliveryPatchDetails[j].fileList[n]);
                        } else {
                            noarray.push(this.stashList[i].deliveryPatchDetails[j].fileList[n]);
                        }

                    }
                }
            } else {
                for (let j = 0; j < this.stashList[i].deliveryPatchDetails.length; j++) {
                    for ( let n = 0; n < this.stashList[i].deliveryPatchDetails[j].fileList.length; n++) {
                        if (this.stashList[i].deliveryPatchDetails[j].fileList[n].checked) {
                            array.push(this.stashList[i].deliveryPatchDetails[j].fileList[n]);
                        } else {
                            noarray.push(this.stashList[i].deliveryPatchDetails[j].fileList[n]);
                        }
                    }
                }
            }

        }
        this.splicingObj = {
            guidDelivery: submitArray, // guid 集合
            deliveryList: array,
            stashList: noarray,
            guidWorkitem: this.workItemInfo.guid,
        };
        
        // 枚举值改变
        for ( let i = 0; i < this.splicingObj.deliveryList.length; i++) {
            if (this.splicingObj.deliveryList[i].commitType === '新增') {
                this.splicingObj.deliveryList[i].commitType = 'A';
            } else if (this.splicingObj.deliveryList[i].commitType === '删除') {
                this.splicingObj.deliveryList[i].commitType = 'D';
            }else if (this.splicingObj.deliveryList[i].commitType === '修改') {
                this.splicingObj.deliveryList[i].commitType = 'M';
            }

            if (this.splicingObj.deliveryList[i].fromType === '手动补录') {
                this.splicingObj.deliveryList[i].fromType = 'A';
            } else if (this.splicingObj.deliveryList[i].fromType === '分支提交') {
                this.splicingObj.deliveryList[i].fromType = 'B';
            }else if (this.splicingObj.deliveryList[i].fromType === '标准清单') {
                this.splicingObj.deliveryList[i].fromType = 'S';
            } else {
                this.splicingObj.deliveryList[i].fromType = 'M';
            }
        }
        for ( let i = 0; i < this.splicingObj.stashList.length; i++) {
            if (this.splicingObj.stashList[i].commitType === '新增') {
                this.splicingObj.stashList[i].commitType = 'A';
            } else if (this.splicingObj.stashList[i].commitType === '删除') {
                this.splicingObj.stashList[i].commitType = 'D';
            }else if (this.splicingObj.stashList[i].commitType === '修改') {
                this.splicingObj.stashList[i].commitType = 'M';
            }

            if (this.splicingObj.stashList[i].fromType === '手动补录') {
                this.splicingObj.stashList[i].fromType = 'A';
            } else if (this.splicingObj.stashList[i].fromType === '分支提交') {
                this.splicingObj.stashList[i].fromType = 'B';
            }else if (this.splicingObj.stashList[i].fromType === '标准清单') {
                this.splicingObj.stashList[i].fromType = 'S';
            } else {
                this.splicingObj.stashList[i].fromType = 'M';
            }
        }
          this.getFalseLoading = true;
        this.utilityService.postData(appConfig.testUrl  + '/sDeliveryList/superadditionDeliverylist', this.splicingObj)
            .subscribe(
                (val) => {
                    this.getFalseLoading = false
                    this.nznot.create('success', val.code , val.msg);
                    this.appendTitle = '追加成功';
                    this.getStatus = true;
                    this.detailInfo = val.result; // 返回的数据有问题
                    this.appendVisible = false;
                    this.reset = false;
                    this.flage = false;
                      this.flageConfig = false;
                      this.flageUpd= false;
                      this.flageUpdConfig = false;
                        if(!this.isgetData){
                        this.isOpen = false;
                    }
                      this.textcssList = []
                      this.stashList = []
                    //   this.checkFlage()
                      this.workItem.deliveries = []
                    this.getcheckOptionOne(this.workItemInfo.guid); // 重新调用清单
                    // this.getData();
                    this.launchVisible = true; // 查看追加详情
                },(error)=>{
                     this.getFalseLoading = false
                        this.nznot.create('error', error.code, error.msg);
                }
            );
    }


    // 调用投放环境接口
    getcheckOptionOne(guid) {
         this.utilityService.getData(appConfig.testUrl  + appConfig.API.sProfiless + '/' + guid + '/delivered'  )
            .subscribe(
                (val) => {
                        this.elementScice = val.result;
                        for (let i = 0; i < this.elementScice.length; i++) {
                            this.elementScice[i].deliveryTime = new Date(this.elementScice[i].deliveryTime); // 初始化时间
                            this.elementScice[i].unixTime = moment(this.elementScice[i].deliveryTime).format('YYYY-MM-DD 00:00:00.000')
                            for (let s = 0; s < this.elementScice[i].packTimeDetails.length; s ++) {
                                if (this.elementScice[i].packTimeDetails[s].isOptions === 'D') {
                                    this.elementScice[i].times = this.elementScice[i].packTimeDetails[s].packTime;
                                }
                            }
                        }
                    localStorage.setItem('name', JSON.stringify(_.cloneDeep(this.elementScice)));
                    },(error)=>{
                  
                        this.nznot.create('error', error.code, error.msg);
                }
                 );
    }

   

    // 比较时间
    onChange(time, array) {
        if (time.getTime() !== new Date(array.unixTime).getTime()) {
           for (let i = 0; i < array.packTimeDetails.length; i++) {
               if (array.packTimeDetails[i].isOptions === 'N') {
                   array.packTimeDetails[i].isOptions = 'No';
               }
           }
        } else {
            for (let i = 0; i < array.packTimeDetails.length; i++) {
                if (array.packTimeDetails[i].isOptions === 'No') {
                    array.packTimeDetails[i].isOptions = 'N';
                }
            }
        }

        if (time.getTime() < new Date(array.unixTime).getTime()) {
            array.deliveryTime = new Date(array.unixTime).getTime();
            this.nznot.create('error', '选择时间不能小于初始时间' , '');
        } else {
        }
    }

    // 清单整理 + 追加清单关闭按钮
    cancel() {

        this.modalVisible = false
        this.getcheckOptionOne(this.workItemInfo.guid); // 重新调用清单
    }

    appcancel() {
        this.appendVisible = false
        this.getcheckOptionOne(this.workItemInfo.guid); // 重新调用清单
    }
}



